/*
 * Created by JFormDesigner on Thu Jun 25 19:28:45 CST 2020
 */

package dice.sinanya.windows;

import dice.sinanya.start.MiraiConfig;
import dice.sinanya.tools.checkdata.CheckIsNumbers;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.*;

import static dice.sinanya.system.MessagesSystem.loginInfo;

/**
 * @author xiaozhou zhang
 */
public class Login extends JDialog {

    public Login(Window owner) {
        super(owner);
        initComponents();
    }

    private void thisWindowClosed(WindowEvent e) {
        System.exit(-1);
    }


    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel dialogPane;
    private JPanel contentPanel;
    private JLabel label1;
    private JLabel label2;
    private JPasswordField jPasswordField1;
    private JFormattedTextField formattedTextField1;
    private JPanel buttonBar;
    private JButton okButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        dialogPane = new JPanel();
        contentPanel = new JPanel();
        label1 = new JLabel();
        label2 = new JLabel();
        jPasswordField1 = new JPasswordField();
        formattedTextField1 = new JFormattedTextField();
        buttonBar = new JPanel();
        okButton = new JButton();

        //======== this ========
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                thisWindowClosed(e);
            }
        });
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== dialogPane ========
        {
            dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
            dialogPane.setLayout(new BorderLayout());

            //======== contentPanel ========
            {
                contentPanel.setLayout(null);

                //---- label1 ----
                label1.setText("QQ\u53f7");
                contentPanel.add(label1);
                label1.setBounds(25, 25, 45, 25);

                //---- label2 ----
                label2.setText("\u5bc6\u7801");
                contentPanel.add(label2);
                label2.setBounds(30, 70, 45, 25);
                contentPanel.add(jPasswordField1);
                jPasswordField1.setBounds(85, 70, 145, jPasswordField1.getPreferredSize().height);

                //---- formattedTextField1 ----
                formattedTextField1.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
                formattedTextField1.addKeyListener(new KeyAdapter() {
                    @Override
                    public void keyPressed(KeyEvent e) {
                        formattedTextField1KeyPressed(e);
                    }
                });
                contentPanel.add(formattedTextField1);
                formattedTextField1.setBounds(85, 25, 145, formattedTextField1.getPreferredSize().height);

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < contentPanel.getComponentCount(); i++) {
                        Rectangle bounds = contentPanel.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = contentPanel.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    contentPanel.setMinimumSize(preferredSize);
                    contentPanel.setPreferredSize(preferredSize);
                }
            }
            dialogPane.add(contentPanel, BorderLayout.NORTH);

            //======== buttonBar ========
            {
                buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
                buttonBar.setLayout(null);

                //---- okButton ----
                okButton.setText("\u767b\u9646");
                okButton.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        loginClick(e);
                    }
                });
                buttonBar.add(okButton);
                okButton.setBounds(new Rectangle(new Point(85, 10), okButton.getPreferredSize()));

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < buttonBar.getComponentCount(); i++) {
                        Rectangle bounds = buttonBar.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = buttonBar.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    buttonBar.setMinimumSize(preferredSize);
                    buttonBar.setPreferredSize(preferredSize);
                }
            }
            dialogPane.add(buttonBar, BorderLayout.SOUTH);
        }
        contentPane.add(dialogPane);
        dialogPane.setBounds(0, 0, 310, 180);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }


    private void loginClick(MouseEvent e) {
        if (!CheckIsNumbers.isNumeric(formattedTextField1.getText())) {
            formattedTextField1.setText("");
            jPasswordField1.setText("");
            return;
        }
        loginInfo.setLoginId(Long.parseLong(formattedTextField1.getText()));
        loginInfo.setPasswd(jPasswordField1.getText());
        this.setVisible(false);
        loginInfo.setInput(false);
        new MiraiConfig().insertLogin();
    }

    private void formattedTextField1KeyPressed(KeyEvent e) {
        loginInfo.setInput(true);
    }
}
